<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="colUserId">
      <el-input v-model="dataForm.colUserId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="开始睡眠时间" prop="colStartSleepTime">
      <el-input v-model="dataForm.colStartSleepTime" placeholder="开始睡眠时间"></el-input>
    </el-form-item>
    <el-form-item label="结束睡眠时间" prop="colEndSleepTime">
      <el-input v-model="dataForm.colEndSleepTime" placeholder="结束睡眠时间"></el-input>
    </el-form-item>
    <el-form-item label="入睡时间" prop="colAsleepTime">
      <el-input v-model="dataForm.colAsleepTime" placeholder="入睡时间"></el-input>
    </el-form-item>
    <el-form-item label="日期" prop="colDate">
      <el-input v-model="dataForm.colDate" placeholder="日期"></el-input>
    </el-form-item>
    <el-form-item label="睡眠评分" prop="colScore">
      <el-input v-model="dataForm.colScore" placeholder="睡眠评分"></el-input>
    </el-form-item>
    <el-form-item label="睡眠总时长（分钟）" prop="colSleepDuration">
      <el-input v-model="dataForm.colSleepDuration" placeholder="睡眠总时长（分钟）"></el-input>
    </el-form-item>
    <el-form-item label="在床时间" prop="colBedDuration">
      <el-input v-model="dataForm.colBedDuration" placeholder="在床时间"></el-input>
    </el-form-item>
    <el-form-item label="睡眠数据" prop="colSleepData">
      <el-input v-model="dataForm.colSleepData" placeholder="睡眠数据"></el-input>
    </el-form-item>
    <el-form-item label="深睡时长" prop="colDeepDuration">
      <el-input v-model="dataForm.colDeepDuration" placeholder="深睡时长"></el-input>
    </el-form-item>
    <el-form-item label="浅睡时长" prop="colLightDuration">
      <el-input v-model="dataForm.colLightDuration" placeholder="浅睡时长"></el-input>
    </el-form-item>
    <el-form-item label="清醒时长" prop="colAwakeDuration">
      <el-input v-model="dataForm.colAwakeDuration" placeholder="清醒时长"></el-input>
    </el-form-item>
    <el-form-item label="音频检测数据" prop="colAudioData">
      <el-input v-model="dataForm.colAudioData" placeholder="音频检测数据"></el-input>
    </el-form-item>
    <el-form-item label="异常数据" prop="colAbnormalAudioData">
      <el-input v-model="dataForm.colAbnormalAudioData" placeholder="异常数据"></el-input>
    </el-form-item>
    <el-form-item label="打鼾次数" prop="colSnoreNum">
      <el-input v-model="dataForm.colSnoreNum" placeholder="打鼾次数"></el-input>
    </el-form-item>
    <el-form-item label="呼吸暂停次数" prop="colApneaNum">
      <el-input v-model="dataForm.colApneaNum" placeholder="呼吸暂停次数"></el-input>
    </el-form-item>
    <el-form-item label="干预次数" prop="colInterventionNum">
      <el-input v-model="dataForm.colInterventionNum" placeholder="干预次数"></el-input>
    </el-form-item>
    <el-form-item label="有效干预次数" prop="colEffectiveInterventionNum">
      <el-input v-model="dataForm.colEffectiveInterventionNum" placeholder="有效干预次数"></el-input>
    </el-form-item>
    <el-form-item label="生成报告所在时区" prop="colCreateZone">
      <el-input v-model="dataForm.colCreateZone" placeholder="生成报告所在时区"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="colIsDelete">
      <el-input v-model="dataForm.colIsDelete" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          colUserId: '',
          colStartSleepTime: '',
          colEndSleepTime: '',
          colAsleepTime: '',
          colDate: '',
          colScore: '',
          colSleepDuration: '',
          colBedDuration: '',
          colSleepData: '',
          colDeepDuration: '',
          colLightDuration: '',
          colAwakeDuration: '',
          colAudioData: '',
          colAbnormalAudioData: '',
          colSnoreNum: '',
          colApneaNum: '',
          colInterventionNum: '',
          colEffectiveInterventionNum: '',
          colCreateZone: '',
          createDate: '',
          colIsDelete: ''
        },
        dataRule: {
          colUserId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          colStartSleepTime: [
            { required: true, message: '开始睡眠时间不能为空', trigger: 'blur' }
          ],
          colEndSleepTime: [
            { required: true, message: '结束睡眠时间不能为空', trigger: 'blur' }
          ],
          colAsleepTime: [
            { required: true, message: '入睡时间不能为空', trigger: 'blur' }
          ],
          colDate: [
            { required: true, message: '日期不能为空', trigger: 'blur' }
          ],
          colScore: [
            { required: true, message: '睡眠评分不能为空', trigger: 'blur' }
          ],
          colSleepDuration: [
            { required: true, message: '睡眠总时长（分钟）不能为空', trigger: 'blur' }
          ],
          colBedDuration: [
            { required: true, message: '在床时间不能为空', trigger: 'blur' }
          ],
          colSleepData: [
            { required: true, message: '睡眠数据不能为空', trigger: 'blur' }
          ],
          colDeepDuration: [
            { required: true, message: '深睡时长不能为空', trigger: 'blur' }
          ],
          colLightDuration: [
            { required: true, message: '浅睡时长不能为空', trigger: 'blur' }
          ],
          colAwakeDuration: [
            { required: true, message: '清醒时长不能为空', trigger: 'blur' }
          ],
          colAudioData: [
            { required: true, message: '音频检测数据不能为空', trigger: 'blur' }
          ],
          colAbnormalAudioData: [
            { required: true, message: '异常数据不能为空', trigger: 'blur' }
          ],
          colSnoreNum: [
            { required: true, message: '打鼾次数不能为空', trigger: 'blur' }
          ],
          colApneaNum: [
            { required: true, message: '呼吸暂停次数不能为空', trigger: 'blur' }
          ],
          colInterventionNum: [
            { required: true, message: '干预次数不能为空', trigger: 'blur' }
          ],
          colEffectiveInterventionNum: [
            { required: true, message: '有效干预次数不能为空', trigger: 'blur' }
          ],
          colCreateZone: [
            { required: true, message: '生成报告所在时区不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          colIsDelete: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/tbsleeprecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.colUserId = data.tbSleepRecord.colUserId
                this.dataForm.colStartSleepTime = data.tbSleepRecord.colStartSleepTime
                this.dataForm.colEndSleepTime = data.tbSleepRecord.colEndSleepTime
                this.dataForm.colAsleepTime = data.tbSleepRecord.colAsleepTime
                this.dataForm.colDate = data.tbSleepRecord.colDate
                this.dataForm.colScore = data.tbSleepRecord.colScore
                this.dataForm.colSleepDuration = data.tbSleepRecord.colSleepDuration
                this.dataForm.colBedDuration = data.tbSleepRecord.colBedDuration
                this.dataForm.colSleepData = data.tbSleepRecord.colSleepData
                this.dataForm.colDeepDuration = data.tbSleepRecord.colDeepDuration
                this.dataForm.colLightDuration = data.tbSleepRecord.colLightDuration
                this.dataForm.colAwakeDuration = data.tbSleepRecord.colAwakeDuration
                this.dataForm.colAudioData = data.tbSleepRecord.colAudioData
                this.dataForm.colAbnormalAudioData = data.tbSleepRecord.colAbnormalAudioData
                this.dataForm.colSnoreNum = data.tbSleepRecord.colSnoreNum
                this.dataForm.colApneaNum = data.tbSleepRecord.colApneaNum
                this.dataForm.colInterventionNum = data.tbSleepRecord.colInterventionNum
                this.dataForm.colEffectiveInterventionNum = data.tbSleepRecord.colEffectiveInterventionNum
                this.dataForm.colCreateZone = data.tbSleepRecord.colCreateZone
                this.dataForm.createDate = data.tbSleepRecord.createDate
                this.dataForm.colIsDelete = data.tbSleepRecord.colIsDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/tbsleeprecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'colUserId': this.dataForm.colUserId,
                'colStartSleepTime': this.dataForm.colStartSleepTime,
                'colEndSleepTime': this.dataForm.colEndSleepTime,
                'colAsleepTime': this.dataForm.colAsleepTime,
                'colDate': this.dataForm.colDate,
                'colScore': this.dataForm.colScore,
                'colSleepDuration': this.dataForm.colSleepDuration,
                'colBedDuration': this.dataForm.colBedDuration,
                'colSleepData': this.dataForm.colSleepData,
                'colDeepDuration': this.dataForm.colDeepDuration,
                'colLightDuration': this.dataForm.colLightDuration,
                'colAwakeDuration': this.dataForm.colAwakeDuration,
                'colAudioData': this.dataForm.colAudioData,
                'colAbnormalAudioData': this.dataForm.colAbnormalAudioData,
                'colSnoreNum': this.dataForm.colSnoreNum,
                'colApneaNum': this.dataForm.colApneaNum,
                'colInterventionNum': this.dataForm.colInterventionNum,
                'colEffectiveInterventionNum': this.dataForm.colEffectiveInterventionNum,
                'colCreateZone': this.dataForm.colCreateZone,
                'createDate': this.dataForm.createDate,
                'colIsDelete': this.dataForm.colIsDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
